﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.XPath;
using System.Net;
using System.IO;

namespace vulnanalyzer
{
    class Download
    {
        public static XPathNavigator GetSiteContent(string uri, Encoding encodingIfSiteEncodingIncorect)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            HttpWebResponse responce = (HttpWebResponse)request.GetResponse();

            Stream st = responce.GetResponseStream();

            string encoding = responce.CharacterSet;
            Encoding bufEnc;
            try
            {
                bufEnc = Encoding.GetEncoding(encoding);
            }
            catch (ArgumentException)
            {
                bufEnc = encodingIfSiteEncodingIncorect;
            }

            StreamReader reader = new StreamReader(st, bufEnc);

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.Load(reader);

            st.Dispose();

            XPathNavigator nav = doc.CreateNavigator();

            return nav;
        }
    }
}
